<?php

class NavController extends BaseController
{

	 /**
	  * 新建菜单
	  */
	 public function actionCreate()
	 {
	 	$model=new Nav;
	 	if(isset($_GET['pid'])){
	 		$model->zna_parentid = (int)$_GET['pid'];
	 	}
	 	$model->zna_type = 3;//'类型：1商家中心，2会员中心，3前台页面'
	 	if(isset($_POST['Nav']))
	 	{
	 		$model->attributes=$_POST['Nav'];
	 		if($model->save())
	 			$this->redirect(array('admin'));
	 	}
	 
	 	$this->render('create',array(
	 			'model'=>$model,
	 	));
	 }

	/**
	 *修改菜单
	 */
	public function actionUpdate($id)
	{
		$model=$this->loadModel($id);

		if(isset($_POST['Nav']))
		{
			$model->attributes=$_POST['Nav'];
			if($model->save())
				$this->redirect(array('admin'));
		}

		$this->render('update',array(
			'model'=>$model,
		));
	}

	/**
	 * 删除菜单
	 * @param integer $id 菜单ID
	 */
	public function actionDelete($id)
	{
		// 删除操作
		$criteria= new CDbCriteria;  
		$criteria->addInCondition('ip_id', $_POST['selectdel']);  
		$this->deleteAll($criteria);
		//成功跳转
		$this->redirect(Yii::app()->request->urlReferrer);
	}

	/**
	 * Lists all models.
	 */
	public function actionIndex()
	{
		$dataProvider=new CActiveDataProvider('Nav');
		$this->render('index',array(
			'dataProvider'=>$dataProvider,
		));
	}

	/**
	 * Manages all models.
	 */
	public function actionAdmin()
	{
		$model=new Nav('search');
		$model->unsetAttributes();  // clear any default values
		$model->zna_type = 3;//'类型：1商家中心，2会员中心，3前台页面'
		if(isset($_GET['Nav']))
			$model->attributes=$_GET['Nav'];

		$this->render('admin',array(
			'model'=>$model,
		));
	}
	
	/**
	 * Returns the data model based on the primary key given in the GET variable.
	 * If the data model is not found, an HTTP exception will be raised.
	 * @param integer the ID of the model to be loaded
	 */
	public function loadModel($id)
	{
		$model=Nav::model()->findByPk($id);
		if($model===null)
			throw new CHttpException(404,'The requested page does not exist.');
		return $model;
	}

	/**
	 * Performs the AJAX validation.
	 * @param CModel the model to be validated
	 */
	protected function performAjaxValidation($model)
	{
		if(isset($_POST['ajax']) && $_POST['ajax']==='nav-form')
		{
			echo CActiveForm::validate($model);
			Yii::app()->end();
		}
	}
	
	/**
	 * 保存
	 */
	 public function actionSave(){
	 	$nav = $_POST['Nav'];
	 	foreach($nav['zna_listorder'] as $key=>$val){
	 		$model = Nav::model()->findByPk($key);
	 		$model->zna_listorder = $val;
	 		$model->zna_url = $nav['zna_url'][$key];
	 		$model->save();
	 	}
	 	//成功跳转
		$this->redirect(Yii::app()->request->urlReferrer);
	 }
	 
	/**
	 * 切换显示隐藏
	 */
	public function actionChangeStatus(){
		$id = (int)trim($_GET['id']);
		$model=$this->loadModel($id);
		$model->zna_status = ($_GET['status']=='1')?2:1;
		$model->save();
		$this->redirect($this->pre_module_url.'/nav/index');
	}

	/**
	 * 异步获取子菜单列表数据
	 */
	public function actionGetChildMenu()
	{
		$data['menuid'] = Yii::app()->request->getParam('menuid',0);
		$data['level']  = Yii::app()->request->getParam('level',1);
		$data['items']  = Nav::model()->getChildData($data['menuid']);
		$data['amount'] = count($data['items']);
		echo json_encode($data);
	}
}
